﻿using MediatR;
using SheepCultivation.Common.Domain.Beginning;
using SheepCultivation.Common.Domain.SheepBasicInformation;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.BeginningW
{
    public class ProgenyPeriodicSeachHandler : IRequestHandler<ProgenyPeriodicSeachCommon, Result>
    {
        public readonly IBaseRepository<ProgenyPeriodic> ProgenyPeriodicRepository;
        public readonly IBaseRepository<BasicInformation> BasicInformationRepository;
        public readonly IBaseRepository<Building> BuildingRepository;
        public readonly IBaseRepository<Columns> ColumnsRepository;

        public ProgenyPeriodicSeachHandler(IBaseRepository<ProgenyPeriodic> progenyPeriodicRepository, IBaseRepository<BasicInformation> basicInformationRepository, IBaseRepository<Building> buildingRepository, IBaseRepository<Columns> columnsRepository)
        {
            ProgenyPeriodicRepository = progenyPeriodicRepository;
            BasicInformationRepository = basicInformationRepository;
            BuildingRepository = buildingRepository;
            ColumnsRepository = columnsRepository;
        }

        public async Task<Result> Handle(ProgenyPeriodicSeachCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
                var list = from a in ProgenyPeriodicRepository.GetAll()
                           join b in BasicInformationRepository.GetAll() on a.BreedingSheepArchiveId equals b.BreedingSheepArchiveId
                           join c in BuildingRepository.GetAll() on a.BuildingId equals c.BuildingId
                           join d in ColumnsRepository.GetAll() on a.ColumnsId equals d.ColumnsId
                           select new PerDto()
                           {
                               ProgenyPeriodicId = a.ProgenyPeriodicId,
                               ProgenyPeriodicReceiptNumber = a.ProgenyPeriodicReceiptNumber,
                               ProgenyPeriodicPeriodicDate = a.ProgenyPeriodicPeriodicDate,
                               BreedingSheepArchiveId = a.BreedingSheepArchiveId,
                               ProgenyPeriodicSavNumber = a.ProgenyPeriodicSavNumber,
                               Auricle = b.Auricle,
                               BuildingId = a.BuildingId,
                               BuildingName = c.BuildingName,
                               ColumnsId = a.ColumnsId,
                               ColumnsName = d.ColumnsName,
                               ProgenyPeriodicStage = a.ProgenyPeriodicStage,
                               ProgenyPeriodicNumber = a.ProgenyPeriodicNumber,
                               ProgenyPeriodicWeight = a.ProgenyPeriodicWeight,
                               Remark = a.Remark
                           };
                res.Data = list;
                res.Code = (int)CommonEnumCode.success;
                res.Msg = "后裔列表展示";
            }
            catch (Exception)
            {

                throw;
            }

            return res;
        }
    }
    public class PerDto
    {
        public int ProgenyPeriodicId { get; set; }
        /// <summary>
        /// 单据号
        /// </summary>
        public string ProgenyPeriodicReceiptNumber { get; set; }
        /// <summary>
        /// 期初日期
        /// </summary>
        public DateTime ProgenyPeriodicPeriodicDate { get; set; }
        /// <summary>
        /// 耳号
        /// </summary>
        public int BreedingSheepArchiveId { get; set; }
        /// <summary>
        /// 流水号
        /// </summary>
        public string ProgenyPeriodicSavNumber { get; set; }
        /// <summary>
        /// 栋舍id
        /// </summary>
        public int BuildingId { get; set; }
        /// <summary>
        /// 阶段
        /// </summary>
        public string ProgenyPeriodicStage { get; set; }
        /// <summary>
        /// 栏位id
        /// </summary>
        public int ColumnsId { get; set; }
        /// <summary>
        /// 只数
        /// </summary>
        public int ProgenyPeriodicNumber { get; set; }
        /// <summary>
        /// 重量
        /// </summary>
        public decimal ProgenyPeriodicWeight { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; }
        /// <summary>
        /// 耳号
        /// </summary>
        public string Auricle { get; set; }
        public string? BuildingName { get; set; }
        public string? ColumnsName { get; set; }
    }
}
